﻿using MapGeneration;
using PluginAPI.Core;
using PluginAPI.Core.Attributes;
using PluginAPI.Enums;
using PluginAPI.Events;
using RoleCustom.Main;
using UnityEngine;
using Utils;


namespace RoleCustom.Event
{
    /// <summary>
    /// 
    /// </summary>
    public class FacilityGuardEvent
    {
        [PluginEvent(ServerEventType.PlayerSpawn)]
        public void OnPlayerSpawn(PlayerSpawnEvent ev)
        {
            TimeCoroutine.Delay(0.5f, () =>
            {
                if (ev.Player.Role != PlayerRoles.RoleTypeId.FacilityGuard) return;

                //PlayerUtils.SetHP(ev.Player,
                //    CodeLoad.Singleton.Config.FacilityGuardEvent.Health,
                //    CodeLoad.Singleton.Config.FacilityGuardEvent.Health);

                PlayerUtils.AddAmmo(ev.Player);

                if (Round.IsLocked) return;

                this.SetSpawnPoint(ev);
                this.SendBroadcast(ev);
            });
        }

        [PluginEvent(ServerEventType.PlayerDeath)]
        public void OnPlayerDeath(PlayerDeathEvent ev)
        {
            if (ev.Player.Role != PlayerRoles.RoleTypeId.FacilityGuard) return;

            ev.Player.ReferenceHub.serverRoles.SetColor("grey");
            ev.Player.ReferenceHub.serverRoles.SetText(string.Empty);
        }


        private void SendBroadcast(PlayerSpawnEvent ev)
        {
            ev.Player.ReferenceHub.serverRoles.SetColor("grey");
            ev.Player.ReferenceHub.serverRoles.SetText(string.Empty);


            //string message = "撤离并保护 <color=#FFFF00>设施员工</color>，阻止 <color=#FFD700>D级人员</color> 的逃离";
            //MessageStringUtils.GernerateMissionMessage(message, out string mission);
            //ev.Player.SendBroadcast(mission, 15, Broadcast.BroadcastFlags.Normal, true);
        }
        private void SetSpawnPoint(PlayerSpawnEvent ev)
        {

            int value = UnityEngine.Random.Range(0, 100);

            if (value >= 90)
            {
                bool isGet = SpawnPointUtils.TryGetSpawnPoint(RoomName.Hcz096, new Vector3(-5.5f, 1f, 0f), out Vector3 output);
                if (isGet)
                {
                    Log.Info($"{ev.Player.Nickname}|保安刷新|{RoomName.Hcz096}");
                    ev.Player.Position = output;
                }
                return;
            }

            if (value >= 80)
            {
                bool isGet = SpawnPointUtils.TryGetSpawnPoint(RoomName.Hcz049, new Vector3(-14, 194f, 0f), out Vector3 output);
                if (isGet)
                {
                    Log.Info($"{ev.Player.Nickname}|保安刷新|{RoomName.Hcz049}");
                    ev.Player.Position = output;
                }
                return;
            }

            //if (value >= 70)
            //{
            //    bool isGet = SpawnPointUtils.TryGetSpawnPoint(RoomName.HczWarhead, new Vector3(-5, 292f, 8f), out Vector3 output);
            //    if (isGet)
            //    {
            //        Log.Info($"{ev.Player.Nickname}|保安刷新|{RoomName.HczWarhead}");
            //        ev.Player.Position = output;
            //    }
            //    return;
            //}
        }
    }
}